package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.Guideline;
import easier.app.x.view.BitmapUtils;
import easier.app.x.view.BitmapUtils.TextBitmapConfig;

public class MainActivity112 extends AppCompatActivity {

    private Guideline guide;
    private ImageView image;
    private ImageView imageWater;
    private EditText input;
    private Button change;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity112.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main112 );
        initView();
        initViewAction();
    }

    private void initView() {

        guide = (Guideline) findViewById( R.id.guide );
        image = (ImageView) findViewById( R.id.image );
        imageWater = (ImageView) findViewById( R.id.imageWater );
        input = (EditText) findViewById( R.id.input );
        change = (Button) findViewById( R.id.change );
    }

    private void initViewAction() {

        image.setImageResource( R.drawable.a5018 );
        change.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                String text = input.getText().toString();
                Drawable drawable = image.getDrawable();
                if ( drawable instanceof BitmapDrawable ) {
                    Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
                    Bitmap toBitmap = BitmapUtils.addTextToBitmap( bitmap, text, new TextBitmapConfig() {
                        @Override
                        public Paint providePaint() {

                            Paint paint = new Paint();
                            paint.setColor( Color.RED );
                            paint.setTextSize( 100 );
                            paint.setAntiAlias( true );
                            paint.setTextAlign( Align.LEFT );
                            return paint;
                        }

                        @Override
                        public float[] decideTextXY( RectF rectF, Paint paint ) {

                            return new float[]{0, 0};
                        }
                    } );
                    imageWater.setImageBitmap( toBitmap );
                }
            }
        } );
    }
}